﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class OptiuniExecutieFrm : FrmCard
    {
        ESRPDataSetExecutie.ExBugSetupRow exSetup = null;
        private Int16 idRaport = 0;

        public OptiuniExecutieFrm(ModuleClass mc, Int16 raport): base(mc)
        {
            InitializeComponent();
            StartDTP.Format = DateTimePickerFormat.Custom;
            StartDTP.CustomFormat = "MM.yyyy";
            EndDTP.Format = DateTimePickerFormat.Custom;
            EndDTP.CustomFormat = "MM.yyyy";
            idRaport = raport;

            switch (idRaport)
            {
                case 0:
                    break;
                case 1:
                    Text = "Optiuni - Cheltuieli totale";
                    break;
                case 2:
                    Text = "Optiuni - Cheltuieli departamente";                    
                    break;
                case 3:
                    Text = "Optiuni - Cheltuieli medicamente pe departamente";     
                    break;
                case 4:
                    Text = "Optiuni - Cheltuieli materiale sanitare pe departamente";
                    break;
                case 5:
                    Text = "Optiuni - Cheltuieli cumulate pe departamente";
                    ToateCB.Visible = true;
                    break;
            }

        }

        private void OptiuniExecutieFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.exBugSetupTableAdapter.Connection = con;
            this.exBugSetupTableAdapter.Fill(this.eSRPDataSetExecutie.ExBugSetup,ModuleInfo.CodUnitate);
            this.balconSetupTableAdapter.Connection = con;
            this.balconSetupTableAdapter.Fill(this.eSRPDataSetExecutie.BalconSetup, ModuleInfo.CodUnitate);
            
            InchideConexiune(con);


            if (exBugSetupBindingSource.Count == 0)
            {
                MessageBoxFrm mbt = new MessageBoxFrm("Mesaj", "Va rugam sa configurati modulul!", String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
                mbt.AddButton("Iesire", 70, mbt.MessageBoxFrm_Click_Yes);
                mbt.ShowDialog();
                this.Close();
                return;
            }
            else
            {
                exSetup = ((DataRowView)exBugSetupBindingSource.Current).Row as ESRPDataSetExecutie.ExBugSetupRow;
            }

            if(exSetup!=null)
            {
                StartDTP.Value = new DateTime(exSetup.AnulCurent, exSetup.LunaCurenta, 1);
                EndDTP.Value = new DateTime(exSetup.AnulCurent, exSetup.LunaCurenta, 1);
            }
            AddButton("Genereaza", global::ERPStudio.Properties.Resources._001_09, Genereaza, ToolStripItemAlignment.Right, false);
        }

        private void Genereaza(object sender, EventArgs e)
        {

            String titlu = "Indicatori pe structuri clinice si asimilate";
            SqlConnection con = DeschideConexiune(workConnString);
            String titlu2 = "in perioada " + StartDTP.Value.Month.ToString() + "." + StartDTP.Value.Year.ToString() + " - " + EndDTP.Value.Month.ToString() + "." + EndDTP.Value.Year.ToString();



            SqlCommand com = new SqlCommand("exbug_UpdateDepartamenteParametrii", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add(new SqlParameter("@CodUnitate",ModuleInfo.CodUnitate));
            com.Parameters.Add(new SqlParameter("@An", StartDTP.Value.Year));
            com.Parameters.Add(new SqlParameter("@Luna", StartDTP.Value.Month));
            com.ExecuteNonQuery();


            PreviewRaportFrm frm = new PreviewRaportFrm("IndicatoriStructuri.rdlc", null, ModuleInfo);
            frm.AddDataSource("ESRPDataSet_BalconSetup", balconSetupBindingSource);
            frm.AddDataSource("ESRPDataSet_ExBugSetup", exBugSetupBindingSource);
            switch (idRaport)
            { 
                case 0:
                    break;
                case 1:
                    frm.NumeRaport = "CheltuieliTotale.rdlc";
                    break;
                case 2:
                    frm.NumeRaport = "CheltuieliDepartamente.rdlc";
                    titlu = "Cheltuieli totale pe departamente";
                    break;
                case 3:
                    frm.NumeRaport = "CheltuieliMedicamenteDepartamente.rdlc";
                    titlu = "Cheltuieli cu medicamente";
                    break;
                case 4:
                    frm.NumeRaport = "CheltuieliMedicamenteDepartamente.rdlc";
                    titlu = "Cheltuieli cu materiale sanitare";
                    break;
                case 5:
                    frm.NumeRaport = "CheltuieliTotaleDepartamente.rdlc";
                    titlu = "Cheltuieli cumulate pe structuri clinice";
                    break;
            }


            
            


            switch(idRaport)
            {
                case 0:
                this.exbug_ListaIndicatoriStructuriTableAdapter.Connection = con;
                this.exbug_ListaIndicatoriStructuriTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaIndicatoriStructuri,
                    StartDTP.Value, EndDTP.Value, ModuleInfo.CodUnitate);
                frm.AddDataSource("ListaIndicatoriStructuri", exbug_ListaIndicatoriStructuriBindingSource);
                break;
                case 1:
                this.exbug_ListaCheltuieliTotaleTableAdapter.Connection = con;
                    this.exbug_ListaCheltuieliTotaleTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaCheltuieliTotale, 
                        ModuleInfo.CodUnitate,
                        StartDTP.Value, EndDTP.Value,-1);
                    frm.AddDataSource("ListaCheltuieliTotale", exbug_ListaCheltuieliTotaleBindingSource);
                break;
                case 2:
                this.exbug_ListaCheltuieliDepartamenteTableAdapter.Connection = con;
                this.exbug_ListaCheltuieliDepartamenteTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaCheltuieliDepartamente,
                    ModuleInfo.CodUnitate,
                    StartDTP.Value, EndDTP.Value, -1);
                frm.AddDataSource("ListaCheltuieliDepartamente", exbug_ListaCheltuieliDepartamenteBindingSource);
                break;
              
                case 3:
                this.exbug_ListaCheltuieliMedicamenteDepartamenteTableAdapter.Connection = con;
                this.exbug_ListaCheltuieliMedicamenteDepartamenteTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaCheltuieliMedicamenteDepartamente,
                    ModuleInfo.CodUnitate,
                    StartDTP.Value,EndDTP.Value, -1);
                frm.AddDataSource("ListaCheltuieliDepartamente", exbug_ListaCheltuieliMedicamenteDepartamenteBindingSource);
                break;

                case 4:
                this.exbug_ListaCheltuieliSanitareDepartamenteTableAdapter.Connection = con;
                this.exbug_ListaCheltuieliSanitareDepartamenteTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaCheltuieliSanitareDepartamente,
                    ModuleInfo.CodUnitate,
                    StartDTP.Value, EndDTP.Value, -1);
                frm.AddDataSource("ListaCheltuieliDepartamente", exbug_ListaCheltuieliSanitareDepartamenteBindingSource);
                break;

                case 5:
                this.exbug_ListaCheltuieliTotaleDepartamenteTableAdapter.Connection = con;
                this.exbug_ListaCheltuieliTotaleDepartamenteTableAdapter.Fill(this.eSRPDataSetExecutie.exbug_ListaCheltuieliTotaleDepartamente,
                    ModuleInfo.CodUnitate,
                    StartDTP.Value, EndDTP.Value, -1,ToateCB.Checked);
                frm.AddDataSource("ListaCheltuieliDepartamente", exbug_ListaCheltuieliTotaleDepartamenteBindingSource);
                break;

            }





            frm.AddParameter("Titlu", titlu, true);
            frm.AddParameter("Titlu2", titlu2, true);
            frm.MdiParent = ModuleInfo.mdiForm;
            frm.Show();
            InchideConexiune(con);

        }

        
    }
}
